package org.example.c4;

import org.example.c2.ICounter;
import org.example.c2.SumTask;

import java.util.concurrent.atomic.LongAdder;

/**
 * @author liu-rui
 * @date 2020/6/8 下午4:39
 * @description
 * 实践LongAdder
 * @since
 */
public class Demo6 {
    public static void main(String[] args) throws InterruptedException {
        SumTask sumTask = new SumTask();
        sumTask.exec(new Counter());
    }

    /**
     * 非线程安全
     */
    static class Counter implements ICounter {
        private LongAdder longAdder = new LongAdder();

        @Override
        public void add(int data) {
            longAdder.add(data);
        }

        @Override
        public int getSum() {
            return (int) longAdder.sum();
        }
    }
}
